package com.xncoding.rocketmq.Listener;

import com.xncoding.rocketmq.constant.RocketConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.support.DefaultRocketMQListenerContainer;
import org.springframework.stereotype.Component;

import java.util.List;

/***
* 添加了 {@link RocketMQMessageListener}之后，即作为一个消费者消费消息
* 相关源码：
* 核心类：{@link DefaultRocketMQListenerContainer}
* 1.ListenerContainerConfiguration这个配置类会注册DefaultRocketMQListenerContainer，见{@link org.apache.rocketmq.spring.autoconfigure.ListenerContainerConfiguration#createRocketMQListenerContainer(String, Object, RocketMQMessageListener)}
* 2.上面那一步会将当前类的RocketMQMessageListener注解解析到传进去，调的就是{@link DefaultRocketMQListenerContainer#setRocketMQMessageListener(RocketMQMessageListener)}
* 3.{@link DefaultRocketMQListenerContainer#initRocketMQPushConsumer}会在这个bean初始化时调用，根据RocketMQMessageListener注解传递的值，设置了各种{@link DefaultMQPushConsumer}的属性
* 4.所以消费者消费消息就是在DefaultRocketMQListenerContainer这里消费的
* 5.咱们当前这个消息监听器就不需要自己处理ack了，DefaultRocketMQListenerContainer内部会自动ack:
* {@link DefaultRocketMQListenerContainer.DefaultMessageListenerConcurrently#consumeMessage(List, ConsumeConcurrentlyContext)}
* {@link DefaultRocketMQListenerContainer.DefaultMessageListenerOrderly#consumeMessage(List, ConsumeOrderlyContext)}
* @description 普通消息的监听器
* @author luming
* @date 2021/4/24 17:05
* @throws
*/
@Slf4j
@Component
@RocketMQMessageListener(consumerGroup = "ConsumerGroupA", topic = RocketConstant.TOPIC_COMMON,consumeMode= ConsumeMode.CONCURRENTLY)
public class CommonMessageListener implements RocketMQListener<String> {
    @Override
    public void onMessage(String message) {
        log.info("Received message:{}",message);
    }
}
